//
//  RegisterViewController.m
//  ShowMe
//
//  Created by 123 on 2018/8/22.
//  Copyright © 2018年 nkl. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import <SMS_SDK/SMSSDK.h>
//倒计时时间
int count = 60;

@interface RegisterViewController ()<registerViewDelegate>
@property(nonatomic,strong) NSTimer * vrTimer;
@property(nonatomic,weak) UIButton *vrSendBtn;
@end

@implementation RegisterViewController
#pragma mark - 懒加载
-(NSTimer *)vrTimer{
    if (!_vrTimer) {
        _vrTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(countDownEvent) userInfo:nil repeats:YES];
    }
    return _vrTimer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

-(void)setUpUI{
    self.navigationController.navigationBar.hidden = YES;
    RegisterView * registView = [[[RegisterView alloc] init] loadLoginViewFromXib];
    registView.delegate = self;
    [self.view addSubview:registView];
    
    [registView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
}

//发送验证码
- (void)sendMessageBtnClick:(UIButton *)sender phoneNumber:(NSString *)phoneNumber{
    
    self.vrSendBtn = sender;
   
    
    if (phoneNumber == nil || [phoneNumber isEqualToString:@""]) {
        [ProgressHUD showSmallErroHUD:@"手机号不能为空"];
        return;
    }
    
    if (![PublicMethod isMobileNumber:phoneNumber]) {
        [ProgressHUD showSmallErroHUD:@"手机格式不正确"];
        return;
    }
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phoneNumber zone:@"86" template:nil result:^(NSError *error) {
        if(!error){
            self.vrSendBtn.enabled = NO;
            self.vrSendBtn.backgroundColor = UIColor.lightGrayColor;
            [self startCountDown];
            [ProgressHUD showSmallSuccessHUD:@"验证码发送成功"];
        }else{
            [ProgressHUD showBigErroHUD:@"验证码发送失败请稍后再试"];
        }
    }];
    
}

-(void)backToLoginViewClick{
    [self.navigationController popViewControllerAnimated:YES];
}
//注册按钮点击
-(void)registCounter:(NSString *)phoneNumber password:(NSString *)password password2:(NSString *)password2 verCode:(NSString *)vercode{

    if ([self judgePassWordBeforeRegister:password second:password2]) {
        
        [SMSSDK commitVerificationCode:vercode phoneNumber:phoneNumber zone:@"86" result:^(NSError *error) {
            if (!error) {
                [DBUserTabelManager registNewCountWith:phoneNumber passWord:password result:^(BOOL isSuccessful, NSError *error) {
                    if (isSuccessful) {
                        [ProgressHUD showSmallSuccessHUD:@"注册成功"];
                        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                    }else{
                        if (error.code == 202) {
                            [ProgressHUD showSmallErroHUD:@"用户已存在"];
                        }else{
                            [ProgressHUD showSmallErroHUD:@""];
                        }
                    }
                }];
            }else{
                [ProgressHUD showBigErroHUD:@"验证码输入错误"];
            }
        }];
    }else{
        [ProgressHUD showBigErroHUD:@"密码不合规范"];
    }
    
    
}
//判断密码是否相同
-(BOOL)judgePassWordBeforeRegister:(NSString *)pwd1 second:(NSString *)pwd2{
    if ([pwd1 isEqualToString:@""] || [pwd2 isEqualToString:@""] || pwd1 == nil || pwd2 == nil) {
        [ProgressHUD showSmallErroHUD:@"密码不能为空"];
        return NO;
    }
    
    if(pwd1.length<6){
        [ProgressHUD showSmallErroHUD:@"密码不能小于6位"];
        return NO;
    }
    
    if ([pwd1 isEqualToString:pwd2]) {
        return YES;
    }else{
        [ProgressHUD showSmallErroHUD:@"两次输入密码不相等"];
        return NO;
    }
    
    
}

//倒计时事件
-(void)startCountDown{
    [[NSRunLoop mainRunLoop] addTimer:self.vrTimer forMode:NSDefaultRunLoopMode];
}

-(void)stopCountDown{
    [self.vrTimer invalidate];
    self.vrTimer = nil;
}

-(void)countDownEvent{
    count -= 1;
    [self.vrSendBtn setTitle:[NSString stringWithFormat:@"%d秒后重发",count] forState:UIControlStateDisabled];
    if (count == 0) {
        [self stopCountDown];
        self.vrSendBtn.enabled = YES;
        self.vrSendBtn.backgroundColor = [UIColor colorWithRed:161/255.0 green:211/255.0 blue:118/255.0 alpha:1];
        count = 60;
        [self.vrSendBtn setTitle:[NSString stringWithFormat:@"%d秒后重发",count] forState:UIControlStateDisabled];
    }
}



@end
